package com.wpc.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.wpc.context.Content;

public class HttpUtil {


	private static String URL_READNUM = "https://mp.weixin.qq.com/mp/getmasssendmsg?";
	private static String USER_AGENTS = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.27.400 QQBrowser/9.0.2524.400";

	
	public static String apacheHttpGet(String biz, String uin, String key){
		HttpClient client = new HttpClient();
		String url = URL_READNUM + "__biz=" + biz + "&uin=" + uin +"&key=" + key;
		 List<Header> headers = new ArrayList<Header>();
	     headers.add(new Header("User-Agent",USER_AGENTS)); 
	     headers.add(new Header("Content-Type", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8;charset=UTF-8")); 
	     headers.add(new Header("Accept-Charset", "UTF-8")); 
	     client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); 
		 HttpMethod method=new GetMethod(url);
		 String htmlStr="";
		 try {
			client.executeMethod(method);
			htmlStr=method.getResponseBodyAsString();
			if(htmlStr.indexOf("title = '失效的验证页面'")>-1){
				return "-1";
			}else{
				if(htmlStr.indexOf("msgList =")>-1 && htmlStr.indexOf("}}]};")>-1){
					htmlStr=htmlStr.substring(htmlStr.indexOf("msgList ="), htmlStr.indexOf("}}]};")+4);
					htmlStr=htmlStr.replace("msgList =", "");
				}else{
					return "-1";
				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			 method.releaseConnection();
		}
		return htmlStr;
	}

	
	public static void httpGetWxkey(int uid, String md5key, Long gzhid, String wxkeyurl){
		HttpClient client = new HttpClient();
		String md5Sign=DigestUtils.md5Hex(uid+md5key);
		String url = wxkeyurl + "sign=" + md5Sign + "&uid=" + uid +"&gzhid=" + gzhid;
		 List<Header> headers = new ArrayList<Header>();
	     headers.add(new Header("Accept-Charset", "UTF-8")); 
	     client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); 
		 HttpMethod method=new GetMethod(url);
		 String htmlStr="";
		 try {
			client.executeMethod(method);
			htmlStr=method.getResponseBodyAsString();
			if(StringUtils.isNotBlank(htmlStr)){
				JSONObject json = JSONObject.parseObject(htmlStr);
				if(json.getString("code").equals("200")){
					Content.WX_KEY = json.getString("wxkey");
					Content.uin = json.getString("uin");
				}
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			 method.releaseConnection();
		}
	}
}